import os

import pytest

import settings

# allure config...
allure_host, allure_port, allure_json_dir, allure_report_dir = \
    settings.ALLURE_HOST, \
    settings.ALLURE_PORT, \
    settings.ALLURE_JSON_DIR, \
    settings.ALLURE_REPORT_DIR

if __name__ == '__main__':
    pytest.main([
        "-vs",
        f"--alluredir={allure_json_dir}",
        f"--clean-alluredir",
        f"--log-format={settings.LOG_FMT}",
        f"--log-date-format={settings.LOG_DATE_FORMAT}",
    ])
    try:
        # 杀死本地端口占用 allure 服务的进程
        # ret = os.popen(f"netstat -nao|findstr {allure_port}")  # 查询占用当前端口的进程
        # string = ret.read().encode('gbk').decode()  # 注意解码方式和cmd要相同，即为"gbk"，否则输出乱码
        # pid = string.split(" ")[-1].strip()  # 获取进程号
        # os.system(f"taskkill -f -pid {pid}")  # 杀死进程
        ...
    except Exception as e:
        raise Exception(f"杀死占用端口进程失败， error: {e}")
    else:
        os.system(f"allure generate {allure_json_dir} --report-dir {allure_report_dir} --clean")
        # os.system(f"allure open {allure_report_dir} --host {allure_host} --port {allure_port}")
